% 3. Representation = 3vs5
% 3-2-3. Quality of Deliberation = 0.250 | Quantity of Deliberation = 36

clear
DS = zeros(1,10000);
Vr = zeros(1,10000);
Am = zeros(1,10000);

for k = 1:10000
    % 1) Setup
    m1 = zeros(12,12);
    for i = 1:18
        m1(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m1;
    m2 = zeros(12,12);
    for i = 1:18
        m2(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m2;
    m3 = zeros(12,12);
    for i = 1:18
        m3(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m3;
    M1 = zeros(12,12);
    for i = 1:18
        M1(randsample(144,1))=abs(normrnd(0,1));
    end
    M1;
    M2 = zeros(12,12);
    for i = 1:18
        M2(randsample(144,1))=abs(normrnd(0,1));
    end
    M2;
    M3 = zeros(12,12);
    for i = 1:18
        M3(randsample(144,1))=abs(normrnd(0,1));
    end
    M3;
    M4 = zeros(12,12);
    for i = 1:18
        M4(randsample(144,1))=abs(normrnd(0,1));
    end
    M4;
    M5 = zeros(12,12);
    for i = 1:18
        M5(randsample(144,1))=abs(normrnd(0,1));
    end
    M5;

    % 2) Deliberation
    % (1) Random selection of a speaker S
    for j = 1:36
        r = rand;
        if r < 0.125 
        S = m1;
    elseif r < 0.25
        S = m2;
    elseif r < 0.375
        S = m3;
    elseif r < 0.5
        S = M1;
    elseif r < 0.625
        S = M2;
    elseif r < 0.75
        S = M3;
    elseif r < 0.875
        S = M4;
    else 
        S = M5;
    end
    % (2) Random selection of a non-zero information point
    info = randsample(nonzeros(S),1);
    % (3) Information sharing and learning
    index = find(S==info);
    if S == m1
        disp('nothing happens')
    elseif S == m2
        m1(index)=m1(index)+0.5*info;
    elseif S == m3
        m1(index)=m1(index)+0.5*info;
    else 
        m1(index)=m1(index)+0.25*info;
    end
    if S == m2
        disp('nothing happens')
    elseif S == m1
        m2(index)=m2(index)+0.5*info;
    elseif S == m3
        m2(index)=m2(index)+0.5*info;
    else
        m2(index)=m2(index)+0.25*info;
    end
    if S == m3
        disp('nothing happens')
    elseif S == m1
        m3(index)=m3(index)+0.5*info;
    elseif S == m2
        m3(index)=m3(index)+0.5*info;
    else
        m3(index)=m3(index)+0.25*info;
    end
    if S == M1
        disp('nothing happens')
    elseif S == m1 
        M1(index)=M1(index)+0.25*info;
    elseif S == m2
        M1(index)=M1(index)+0.25*info;
    elseif S == m3
        M1(index)=M1(index)+0.25*info;    
    else 
        M1(index)=M1(index)+0.5*info;
    end
    if S == M2
        disp('nothing happens')
    elseif S == m1
        M2(index)=M2(index)+0.25*info;
    elseif S == m2
        M2(index)=M2(index)+0.25*info;
    elseif S == m3
        M2(index)=M2(index)+0.25*info;
    else 
        M2(index)=M2(index)+0.5*info;
    end
    if S == M3
        disp('nothing happens')
    elseif S == m1
        M3(index)=M3(index)+0.25*info;
    elseif S == m2
        M3(index)=M3(index)+0.25*info;
    elseif S == m3
        M3(index)=M3(index)+0.25*info;
    else 
        M3(index)=M3(index)+0.5*info;
    end
    if S == M4
        disp('nothing happens')
    elseif S == m1
        M4(index)=M4(index)+0.25*info;
    elseif S == m2
        M4(index)=M4(index)+0.25*info;
    elseif S == m3
        M4(index)=M4(index)+0.25*info;
    else 
        M4(index)=M4(index)+0.5*info;
    end
    if S == M5
        disp('nothing happens')
    elseif S == m1
        M5(index)=M5(index)+0.25*info;
    elseif S == m2
        M5(index)=M5(index)+0.25*info;
    elseif S == m3
        M5(index)=M5(index)+0.25*info;
    else 
        M5(index)=M5(index)+0.5*info;
    end
    end

    % 3) Voting
    % (1) Generating policy proposal
    PP = ones(1,12);
    m11 = sum(m1(:,1));
    m21 = sum(m2(:,1));
    m31 = sum(m3(:,1));
    M11 = sum(M1(:,1));
    M21 = sum(M2(:,1));
    M31 = sum(M3(:,1));
    M41 = sum(M4(:,1));
    M51 = sum(M5(:,1));
    PD1 = [m11, m21, m31, M11, M21, M31, M41, M51];
    if sum(PD1>0) >= 6
        PP(1,1) = 1;
    else 
        PP(1,1) = -1;
    end
    m12 = sum(m1(:,2));
    m22 = sum(m2(:,2));
    m32 = sum(m3(:,2));
    M12 = sum(M1(:,2));
    M22 = sum(M2(:,2));
    M32 = sum(M3(:,2));
    M42 = sum(M4(:,2));
    M52 = sum(M5(:,2));
    PD2 = [m12, m22, m32, M12, M22, M32, M42, M52];
    if sum(PD2>0) >= 6
        PP(1,2) = 1;
    else 
        PP(1,2) = -1;
    end
    m13 = sum(m1(:,3));
    m23 = sum(m2(:,3));
    m33 = sum(m3(:,3));
    M13 = sum(M1(:,3));
    M23 = sum(M2(:,3));
    M33 = sum(M3(:,3));
    M43 = sum(M4(:,3));
    M53 = sum(M5(:,3));
    PD3 = [m13, m23, m33, M13, M23, M33, M43, M53];
    if sum(PD3>0) >= 6
        PP(1,3) = 1;
    else 
        PP(1,3) = -1;
    end
    m14 = sum(m1(:,4));
    m24 = sum(m2(:,4));
    m34 = sum(m3(:,4));
    M14 = sum(M1(:,4));
    M24 = sum(M2(:,4));
    M34 = sum(M3(:,4));
    M44 = sum(M4(:,4));
    M54 = sum(M5(:,4));
    PD4 = [m14, m24, m34, M14, M24, M34, M44, M54];
    if sum(PD4>0) >= 6
        PP(1,4) = 1;
    else 
        PP(1,4) = -1;
    end
    m15 = sum(m1(:,5));
    m25 = sum(m2(:,5));
    m35 = sum(m3(:,5));
    M15 = sum(M1(:,5));
    M25 = sum(M2(:,5));
    M35 = sum(M3(:,5));
    M45 = sum(M4(:,5));
    M55 = sum(M5(:,5));
    PD5 = [m15, m25, m35, M15, M25, M35, M45, M55];
    if sum(PD5>0) >= 6
        PP(1,5) = 1;
    else 
        PP(1,5) = -1;
    end
    m16 = sum(m1(:,6));
    m26 = sum(m2(:,6));
    m36 = sum(m3(:,6));
    M16 = sum(M1(:,6));
    M26 = sum(M2(:,6));
    M36 = sum(M3(:,6));
    M46 = sum(M4(:,6));
    M56 = sum(M5(:,6));
    PD6 = [m16, m26, m36, M16, M26, M36, M46, M56];
    if sum(PD6>0) >= 6
        PP(1,6) = 1;
    else 
        PP(1,6) = -1;
    end
    m17 = sum(m1(:,7));
    m27 = sum(m2(:,7));
    m37 = sum(m3(:,7));
    M17 = sum(M1(:,7));
    M27 = sum(M2(:,7));
    M37 = sum(M3(:,7));
    M47 = sum(M4(:,7));
    M57 = sum(M5(:,7));
    PD7 = [m17, m27, m37, M17, M27, M37, M47, M57];
    if sum(PD7>0) >= 6
        PP(1,7) = 1;
    else 
        PP(1,7) = -1;
    end
    m18 = sum(m1(:,8));
    m28 = sum(m2(:,8));
    m38 = sum(m3(:,8));
    M18 = sum(M1(:,8));
    M28 = sum(M2(:,8));
    M38 = sum(M3(:,8));
    M48 = sum(M4(:,8));
    M58 = sum(M5(:,8));
    PD8 = [m18, m28, m38, M18, M28, M38, M48, M58];
    if sum(PD8>0) >= 6
        PP(1,8) = 1;
    else 
        PP(1,8) = -1;
    end
    m19 = sum(m1(:,9));
    m29 = sum(m2(:,9));
    m39 = sum(m3(:,9));
    M19 = sum(M1(:,9));
    M29 = sum(M2(:,9));
    M39 = sum(M3(:,9));
    M49 = sum(M4(:,9));
    M59 = sum(M5(:,9));
    PD9 = [m19, m29, m39, M19, M29, M39, M49, M59];
    if sum(PD9>0) >= 6
        PP(1,9) = 1;
    else 
        PP(1,9) = -1;
    end
    m110 = sum(m1(:,10));
    m210 = sum(m2(:,10));
    m310 = sum(m3(:,10));
    M110 = sum(M1(:,10));
    M210 = sum(M2(:,10));
    M310 = sum(M3(:,10));
    M410 = sum(M4(:,10));
    M510 = sum(M5(:,10));
    PD10 = [m110, m210, m310, M110, M210, M310, M410, M510];
    if sum(PD10>0) >= 6
        PP(1,10) = 1;
    else 
        PP(1,10) = -1;
    end
    m111 = sum(m1(:,11));
    m211 = sum(m2(:,11));
    m311 = sum(m3(:,11));
    M111 = sum(M1(:,11));
    M211 = sum(M2(:,11));
    M311 = sum(M3(:,11));
    M411 = sum(M4(:,11));
    M511 = sum(M5(:,11));
    PD11 = [m111, m211, m311, M111, M211, M311, M411, M511];
    if sum(PD11>0) >= 6
        PP(1,11) = 1;
    else 
        PP(1,11) = -1;
    end
    m112 = sum(m1(:,12));
    m212 = sum(m2(:,12));
    m312 = sum(m3(:,12));
    M112 = sum(M1(:,12));
    M212 = sum(M2(:,12));
    M312 = sum(M3(:,12));
    M412 = sum(M4(:,12));
    M512 = sum(M5(:,12));
    PD12 = [m112, m212, m312, M112, M212, M312, M412, M512];
    if sum(PD12>0) >= 6
        PP(1,12) = 1;
    else 
        PP(1,12) = -1;
    end
    PP;
    % (2) Acceptability of policy proposal
    Am1 = m11*PP(1,1)+m12*PP(1,2)+m13*PP(1,3)+m14*PP(1,4)+m15*PP(1,5)+m16*PP(1,6)+m17*PP(1,7)+m18*PP(1,8)+m19*PP(1,9)+m110*PP(1,10)+m111*PP(1,11)+m112*PP(1,12);
    Am2 = m21*PP(1,1)+m22*PP(1,2)+m23*PP(1,3)+m24*PP(1,4)+m25*PP(1,5)+m26*PP(1,6)+m27*PP(1,7)+m28*PP(1,8)+m29*PP(1,9)+m210*PP(1,10)+m211*PP(1,11)+m212*PP(1,12);
    Am3 = m31*PP(1,1)+m32*PP(1,2)+m33*PP(1,3)+m34*PP(1,4)+m35*PP(1,5)+m36*PP(1,6)+m37*PP(1,7)+m38*PP(1,8)+m39*PP(1,9)+m310*PP(1,10)+m311*PP(1,11)+m312*PP(1,12);
    AM1 = M11*PP(1,1)+M12*PP(1,2)+M13*PP(1,3)+M14*PP(1,4)+M15*PP(1,5)+M16*PP(1,6)+M17*PP(1,7)+M18*PP(1,8)+M19*PP(1,9)+M110*PP(1,10)+M111*PP(1,11)+M112*PP(1,12);
    AM2 = M21*PP(1,1)+M22*PP(1,2)+M23*PP(1,3)+M24*PP(1,4)+M25*PP(1,5)+M26*PP(1,6)+M27*PP(1,7)+M28*PP(1,8)+M29*PP(1,9)+M210*PP(1,10)+M211*PP(1,11)+M212*PP(1,12);
    AM3 = M31*PP(1,1)+M32*PP(1,2)+M33*PP(1,3)+M34*PP(1,4)+M35*PP(1,5)+M36*PP(1,6)+M37*PP(1,7)+M38*PP(1,8)+M39*PP(1,9)+M310*PP(1,10)+M311*PP(1,11)+M312*PP(1,12);
    AM4 = M41*PP(1,1)+M42*PP(1,2)+M43*PP(1,3)+M44*PP(1,4)+M45*PP(1,5)+M46*PP(1,6)+M47*PP(1,7)+M48*PP(1,8)+M49*PP(1,9)+M410*PP(1,10)+M411*PP(1,11)+M412*PP(1,12);
    AM5 = M51*PP(1,1)+M52*PP(1,2)+M53*PP(1,3)+M54*PP(1,4)+M55*PP(1,5)+M56*PP(1,6)+M57*PP(1,7)+M58*PP(1,8)+M59*PP(1,9)+M510*PP(1,10)+M511*PP(1,11)+M512*PP(1,12);

    % (3) Consensus decision rule
    if Am1>=0 && Am2>=0 && Am3>=0 && AM1>=0 && AM2>=0 && AM3>=0 && AM4>=0 && AM5>=0
        DS(1,k) = 1;
    else 
        DS(1,k) = 0;
    end
    
    % 4) Dependent variables
    Vr(1,k) = sum(PP<0)/12;
    if DS(1,k) == 1
        VrxDS(1,k) = Vr(1,k)*100;
    else
        VrxDS(1,k) = NaN;
    end

    Am(1,k) = (sum(sum(Am1))+sum(sum(Am2))+sum(Am3))/12;
    if DS(1,k) == 1
        AmxDS(1,k) = Am(1,k);
    else
        AmxDS(1,k) = NaN;
    end

    AM(1,k) = (sum(AM1)+sum(AM2)+sum(AM3)+sum(AM4)+sum(AM5))/12;
    if DS(1,k) == 1
        AMxDS(1,k) = AM(1,k);
    else
        AMxDS(1,k) = NaN;
    end
end

writematrix(VrxDS','Vrd3t2.xls')

writematrix(AmxDS','Amd3t2.xls')

RAd = AmxDS./AMxDS;
writematrix(RAd','RAd3t2.xls')

mean(DS)*100
nanmean(VrxDS)
nanstd(VrxDS)
nanmean(AmxDS)
nanstd(AmxDS)
nanmean(RAd)*100
nanstd(RAd)
nanstd(RAd)*100